package com.example.aircraft.panel;




import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;

import android.view.View;
import android.view.Window;

import android.widget.EditText;
import android.widget.Toast;


import com.example.aircraft.MyApplication;
import com.example.aircraft.R;
import com.example.aircraft.database.MySqlManager;
import com.example.aircraft.database.MySqliteHelper;
import com.example.aircraft.database.SQLiteInterface;
import com.example.aircraft.database.User_Data;

import java.util.List;
//单机登录界面
public class SignPanel extends Activity {
    private MySqliteHelper helper;
    private SQLiteDatabase db;
    private static User_Data user_data=null;
    private List<User_Data> userList;
    private static String userName;
    private int credits = 0;

//    private static String passWord;

    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        helper = MySqlManager.getInstance(MyApplication.getContext());


        setContentView(R.layout.activity_sign_panel);
        EditText editText1 = findViewById(R.id.editTextAccount1);
        SpannableString ss1 = new SpannableString("请输入昵称");
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(15,true);
        ss1.setSpan(ass,0,ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        editText1.setHint(new SpannableString(ss1));
    }

    public void SignIn (View view){
        //获取输入的用户名
        db = helper.getReadableDatabase();
        String selectsql = "select * from "+ SQLiteInterface.TABLE2_NAME;
        userList = MySqlManager.cursor2ToList(MySqlManager.selectSQL(db, selectsql, null));
        boolean success = false;
        EditText accountText= findViewById(R.id.editTextAccount1);
        userName = accountText.getText().toString();
        if(userName.equals("")){
            Toast toastCenter = Toast.makeText(this,"昵称为空",Toast.LENGTH_SHORT);
            toastCenter.show();
        }else{
            for(User_Data user:userList){
                //&&user.getPASS_WORD().equals(passWord)
                if(user.getUSER_NAME().equals(userName)){
                    user_data = user;
                    success = true;
                    Toast toastCenter = Toast.makeText(this,"欢迎回来"+userName,Toast.LENGTH_SHORT);
                    toastCenter.show();
                    Intent intent = new Intent(this, MenuActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
            if(!success){
                ContentValues values = new ContentValues();
                values.put(SQLiteInterface.USER_NAME, userName);
                values.put(SQLiteInterface.CREDITS,this.credits);
                values.put(SQLiteInterface.CLUSTER,0);
                values.put(SQLiteInterface.HEMOPHAGIA,0);
                values.put(SQLiteInterface.SHIELD,0);
                if(db.insert(SQLiteInterface.TABLE2_NAME,null,values)>=0){
                    user_data = new User_Data(userName,0,0,0,0);
                    Toast.makeText(this,"欢迎新玩家",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(this, MenuActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }

    }

    public static String getUserName(){
        return userName;
    }

    public static User_Data getUser_data(){
        return user_data;
    }


}